export function sleep(ms) {
    return new Promise(resolve => {
        setTimeout(resolve, ms)
    });
}

export function captureMouse(element) {
    let mouse = { x: 0, y: 0 }
    element.addEventListener('mousemove', (event) => {
        // mouse.x = 

        let x = event.pageX
        let y = event.pageY

        let { left, top } = element.getBoundingClientRect()

        x -= left
        y -= top

        mouse.x = x
        mouse.y = y
    });

    return mouse
}


export function containsPoint(rect, x, y) {
    let w = rect.w || rect.width
    let h = rect.h || rect.height
    return x > rect.x && x < rect.x + w && y > rect.y && y < rect.y + h
}

export function randomColor() {
    let color = Math.random() * 0xffffff;
    let r = color >> 16 & 0xff;
    let g = color >> 8 & 0xff;
    let b = color & 0xff;
    let c = `rgb(${r}, ${g}, ${b})`;
    return c
}

export function rgbToRgba(rgbColor, alpha = 1) {
    // let hex = (r << 16 | g << 8 | b).toString(16)
    let reg = /^rgb\((\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3})\)$/;
    let match = rgbColor.match(reg);
    let r = 0, g = 0, b = 0, a = alpha;
    if (match) {
        r = match[1];
        g = match[2];
        b = match[3];
    }
    let color = `rgba(${r}, ${g}, ${b}, ${a})`;
    return color;

}
